/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.objects.text;

import org.easyway.annotations.Optimized;
import org.easyway.interfaces.extended.IDrawing;
import org.easyway.interfaces.extended.ILayerID;
import org.easyway.interfaces.extended.ILoopable;
import org.easyway.interfaces.sprites.IFont;
import org.easyway.objects.BaseObject;
import org.easyway.system.StaticRef;
import org.easyway.utils.Utility;
import org.lwjgl.opengl.GL11;
@Optimized
public class EWText extends BaseObject implements IDrawing, ILayerID {

	static final long serialVersionUID = 22;

	// private EWStaticFont sfont;

	private IFont font;

	private String text;

	public static EWFont standardFont = null;

	public static EWStaticFont standardStaticFont = null;

	public int x, y;

	public int width;

	public float red = 1, green = 1, blue = 1;

	public int size = 18;

	private int layer = 0;
	/**
	 * the drawing sheet
	 */
	private int idLayer = -1;

	// --------------------------- COSTRUTTORI -----------------

	public EWText() {
		this(null, 0, 0, "", 24, 0);
	}

	public EWText(String text) {
		this(null, 0, 0, text, 24, 0);
	}

	public EWText(int x, int y, String text, int size) {
		this(null, x, y, text, size, 0);
	}

	public EWText(EWFont font, int x, int y, String text, int size) {
		this(font, x, y, text, size, 0);
	}

	public EWText(EWFont font, int x, int y, String text, int size, int width) {
		super(false);
		addToIDLayer();
		this.font = (font == null) ? IFont.defaultFont : font;
		this.size = size;
		this.text = text;
		this.x = x;
		this.y = y;
		this.width = width;
		type.set("$_TEXT");
	}

	public EWText(EWStaticFont font, String text, int x, int y) {
		this(font, text, x, y, 0);
	}

	public EWText(EWStaticFont font, String text, int x, int y, int width) {
		super(false);
		addToIDLayer();
		if (font == null) {
			if (standardStaticFont == null)
				standardStaticFont = new EWStaticFont("Arial", 20);
			this.font = standardStaticFont;
		} else {
			this.font = font;
		}

		this.text = text;
		this.x = x;
		this.y = y;
		this.width = width;
	}

	// --------------------------- STATICS -----------------

	public static String createColor(int red, int green, int blue) {
		return "%" + (char) red + (char) green + (char) blue;
	}

	public final static void generateStandardFont() {
		if (standardFont == null)
			standardFont = new EWFont("Arial");
		if (standardStaticFont == null)
			standardStaticFont = new EWStaticFont("Arial", 20);
	}

	// --------------------------- USERS -----------------

	public String getText() {
		return text;
	}

	public void setText(String text) {
		// if (text != null)
		this.text = text;
	}

	protected void addToIDLayer() {
		//StaticRef.layers[6].add(this);
		setIdLayer(StaticRef.layers.length-1);
		if (isLoopable) {
			StaticRef.loopList.add((ILoopable)this);
		}
	}

	public void setColor(float red, float green, float blue) {
		this.red = red;
		this.blue = blue;
		this.green = green;
	}

	public void setLayer(int layer) {
		this.layer = layer;
		readdToDrawingLists();
	}

	public int getLayer() {
		return layer;
	}

	// ----------------------------- SYSTEM ------------

	public void render() {
		if (text == null) // invisibile
			return;
		assert font != null : "null font";
		if (font != null) {
			boolean mustchange = false;
			if (StaticRef.getCamera().is3D()) {
				mustchange = true;
				StaticRef.getCamera().set2D();
			}
			// if (GL11.glIsEnabled(GL11.GL_BLEND))
			// GL11.glDisable(GL11.GL_BLEND);
			GL11.glColor4f(red, green, blue, 1);
			font.setSize(size);
			if (width == 0)
				font.writeString(text, text.length(), x, y);
			else
				font.writeStringRect(text, text.length(), x, y, width);
			if (mustchange)
				StaticRef.getCamera().set3D();
		} else {
			Utility.error("null font", "EWText.render");
		}
	}

	// -----------------------------------------------------------------
	// ---------------------------- IDLAYER-----------------------------
	// -----------------------------------------------------------------


	public int getIdLayer() {
		return idLayer;
	}

	public void setIdLayer(int id) {
		if (idLayer != -1) {
			StaticRef.layers[idLayer].remove(this);
		}
		if (id < 0) {
			id = 0;
		} else if (id > StaticRef.layers.length) {
			id = StaticRef.layers.length;
		}
		idLayer = id;
		StaticRef.layers[idLayer].add(this);
	}

}
